Skip to content

322 sequenced data split - #323

Open
Raindrop182 wants to merge 6 commits into
devfrom
322-sequenced-data-split
Open

322 sequenced data split#323
Raindrop182 wants to merge 6 commits into
devfrom
322-sequenced-data-split

Conversation

@Raindrop182

Copy link
Copy Markdown
Collaborator

Edits export_train_val_test to contain an optional group_col parameter. If group_col is not None, a new helper function _stratified_grouped_datasplit is called, which returns train_df, val_df, and test_df such that every group in group_col belongs to only one of test/train/val and label_col is stratified as best as possible. This allows for data splits that prevent data leakage by keeping images from the same sequence/camera location together.

It does this by utilizing sklearn's StratifiedGroupKFold function. StratifiedGroupKFold splits data into n_splits equal-sized sections, ensuring that each group in group_col belongs to only one section, and that label_col is stratified as best as possible. The helper function then grabs a certain number of sections to form the test and val datasets. (The logic is a bit unstraightforward because StratifiedGroupKFold's primary purpose is for k-fold cross validation)

Some limitations:

  • Since StratifiedGroupKFold splits the data into an integer n_splits sections, test_size and val_size are rounded to the nearest 0.05
  • StratifiedGroupKFold makes sure that each group in group_col belongs to only one of train/val/test. It tries to stratify label_col as best as possible, but it may not be mathematically possible for it to create a completely even distribution.
  • It doesn't accept test_size=0 or val_size=0, since it rounds them up to 0.05. However, the original export_train_val_test function also doesn't work for zero values since sklearn's train_test_split function raises an InvalidParameterError if test_size is 0.

Closes #322

sklearn's train_test_split function raises an
InvalidParameterError when test_size or val_size are zero.
@Raindrop182

Copy link
Copy Markdown
Collaborator Author

If we decide to add this feature, I can add a commit with unit tests. I have confirmed the code works, as I used it to split data for training already.

@Raindrop182
Raindrop182 requested a review from tkswanson July 7, 2026 17:38
@tkswanson
tkswanson marked this pull request as ready for review July 29, 2026 17:35
Comment thread src/animl/export.py Outdated
val_size: float = 0.1,
test_size: float = 0.1,
seed: int = 42,
groupby_col: str = "sequence"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets not set the default groupby_col, especially since train_test_val defaults to None. Instead let's suggest using sequence in the doc string

necessary to fix bug where if the manifest had a conf_col
but was passed in with duplicate indices, this could
potentially mess up the process of keeping only
the highest confidence entry (ex: two rows with the same
filepath, same indices, but different confidences would be kept
instead of removed)
@Raindrop182
Raindrop182 requested a review from tkswanson July 30, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants